home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / GoodBye / JohnKennedy / November94.lha / NovMaster.doc < prev    next >
Encoding:
Text File  |  1994-09-13  |  10.2 KB  |  368 lines

  1. Masterclass November
  2.  
  3.  
  4. Sometimes even AmigaDOS isn't enough - John Kennedy moves
  5. onto ARexx.
  6.  
  7.  
  8.  
  9.  
  10.  
  11. The idea for this month's Masterclass came from a letter
  12. from Duncan Strand from Leicester.  Duncan has several disks
  13. full of hundreds of sound samples, and none of them has an
  14. icon - which can make orginising them a bit of a problem.
  15.  
  16. Duncan has been experimenting with AmigaDOS, trying to
  17. create multiple icons by performing variations with COPY and
  18. the pattern matching - none of which have worked.
  19.  
  20. Now it would be possible to write an AmigaDOS script to
  21. create all the files, but to be honest I'm not a big fan of
  22. writing cumbersome programs in AmigaDOS.  I know there are
  23. big fans out there, but I'm not one of them.  When I want to
  24. write a program, I want to use a programming language:  not
  25. a series of DOS commands.
  26.  
  27. All Workbench 2.04 and better Amigas come with ARexx
  28. present.  ARexx is the Amiga implementation of Rexx, a
  29. language which has been around for a bit.  In fact, the
  30. alternative IBM-PC operating system, OS/2 comes with Rexx,
  31. although it's use is even less wide spread than the Amiga.
  32.  
  33. ARexx is a little like AmigaDOS in that its programs are
  34. stored in plain text files, and there is no need to compile
  35. them first.  ARexx is an interpreted language, and although
  36. this stops it from being the fastest language in the world,
  37. it also makes it one of the simpler one to use.
  38.  
  39.  
  40.  
  41.  
  42. Starting ARexx
  43. --------------
  44.  
  45. Before you can use any ARexx programs, you need to make sure
  46. the main ARexx interpreter program is present and running.
  47. The name of this program is Rexxmast, and you'll find it in
  48. the System drawer of the Workbench drawer.  You can start it
  49. by clicking on it, or run it from the SHELL by entering
  50. Rexxmast and pressing return.  If you want to make it run
  51. automatically every time you boot your computer (which is
  52. especially handy if you have a hard drive, when the
  53. overheads will be negligible) you should add it to the file
  54. called 'user-startup' in the s:  directory.  If this file
  55. doesn't already exist, you can create it by entering:
  56.  
  57. ed s:user-startup
  58.  
  59. at the Shell.  If the file does already exist it will be
  60. loaded, and you'll be able to add more text.  In either
  61. case, add the following:
  62.  
  63. run >nil: <nil: rexxmast
  64.  
  65. The '>nil:' and '<nil' parts will ensure that a new window
  66. isn't opened by Rexxmast when it starts, but you can leave
  67. them out if you so desire.  If using the standard Commodore
  68. ED, remember to press ESC and then X to quit.
  69.  
  70.  
  71.  
  72. Using ARexx
  73. -----------
  74.  
  75.  
  76. To start an ARexx program, you use the RX command which has
  77. been lurking on your Workbench all this time.  So to run an
  78. ARexx program called 'Test.rexx' (it is convention name it
  79. ending in .rexx') you simply open a Shell and type:
  80.  
  81. rx test.rexx
  82.  
  83. or
  84.  
  85. rx test
  86.  
  87. as it will assume the .rexx extension if it is left out.
  88.  
  89. Now that's how we run a program, but how to we write one?
  90. In the same we used ED to alter (or create) the user-startup
  91. file, we also create a file containing ARexx commands.  At
  92. this point it is worth pointing out that ED is by all
  93. accounts an extremely primitive text editor, and one which
  94. you don't have to put up with.  If your finances can't
  95. stretch to buying Cygnus Ed (CED), you can always get GoldED
  96. - a superb text editor which is available from many public
  97. domain libraries.
  98.  
  99. Writing ARexx programs isn't particularly difficult - but
  100. you do need to know the relevant ARexx commands.
  101. Unfortunately Commodore decided to cut a few costs, and
  102. unless you have an A4000 you probably won't have the
  103. necessary documentation.  There are several books on the
  104. subject, and I recommend the Abacus guide 'Using ARexx on
  105. the Amiga' by Zamara and Sullivan, wholeheartedly.
  106.  
  107. I've listed a few of the most important ARexx commands in
  108. table 1, and I hope the construction of our example program
  109. serves to illustrate the grammar required by ARexx.
  110.  
  111. Another important point to realise is that ARexx can have
  112. many more commands than those in table 1.  In fact, any
  113. program with an ARexx 'Port' will add its own commands.
  114.  
  115. The Port is a special piece of programming included in the
  116. original program which makes available various features.
  117. Many commercial and public domain utilities will offer this
  118. facility and in this way ARexx can be used to link several
  119. programs together.  For example, if both an image processing
  120. program and the software which controls a video digitiser
  121. have ARexx ports, it is possible to write a script that will
  122. get the digitiser to grab a frame and then pass it
  123. automatically to the image processor for further work.
  124. ARexx thus gives any program with a suitable port an
  125. extensive Macro facility and also allows it to truly make
  126. use of the Amiga's multitasking capabilities.  Even now, the
  127. Mac and IBM-PC are only starting to wake up to the
  128. possibilities offered by such a system.  The facility to
  129. create macros (in other words, the ability to pre-program a
  130. sequence of events) is terrific when a large number of
  131. similar operations need to be carried out.
  132.  
  133. Here then is a summary of what ARexx is good at:
  134.  
  135. 1.  Writing small stand-alone programs,
  136.  
  137. 2.  Using the ARexx port present in many programs to create
  138. macros and automate tedious processes,
  139.  
  140. 3.  Linking together several separate programs, making use of
  141. various features from each.
  142.  
  143.  
  144.  
  145. Writing ARexx Programs
  146. ----------------------
  147.  
  148. The first line of an ARexx program must start with a
  149. comment.  This isn't just a Good Idea suggested by the
  150. designers of Rexx, but something which has to be done or the
  151. program won't work.  Like C, comments are contained within
  152. /* and */ symbols.
  153.  
  154. Sometimes you'll see the ARexx word ADDRESS used.  This
  155. specifies which other programs the ARexx system will look
  156. too for executing words.  If, for example, you were writing
  157. a script that made use of ASDG's The Art Department, you
  158. would need to specify address 'adpro' in the ARexx program
  159. (the case of the port name is imporant).  The only exception
  160. to this is when the ARexx program is executed not with the
  161. RX command, but from with the calling program (for example,
  162. Art Department) itself.  It doesn't do any harm to keep the
  163. address line in.
  164.  
  165. Using the word COMMAND after address is another exception,
  166. and this time instructs ARexx to use AmigaDOS functions.
  167.  
  168.  
  169. In any case, here is a small program to make sure that the
  170. ARexx system is working. Enter the following into a text
  171. file (typing it directly into the Shell will not work):
  172.  
  173. /* Test program one */
  174.  
  175. say "Hello, World" 
  176.  
  177. do 10
  178.     say "*"
  179. end
  180.  
  181.  
  182.  
  183. Now we come to creating the icons. The easiest way to do
  184. this is to create a dummy icon (I've called it dummy.info).
  185. A loop in the program then creates a host of new filenames
  186. using the loop counter to create the names, which will be
  187. sound1.info, sound2.info, sound3.info and so on. You will
  188. need to experiment with pathnames to suit your system.
  189.  
  190.  
  191. /* Copy program one */
  192.  
  193. address command
  194.  
  195. do i=1 to 10
  196.  
  197.     name="sound" || i || ".info"
  198.  
  199.     com="copy dummy.info" name
  200.  
  201.     com
  202.  
  203. end
  204.  
  205.  
  206.  
  207. The script is short and sweet, but it makes the assumption
  208. that the samples are already named sound1, sound2, sound3 to
  209. start with, which probably isn't the case.
  210.  
  211. What we need is an ARexx program that will create an icon
  212. for a file of any name, and here is how to do it. First of
  213. all we need to rewrite the script to make use of the ARG and
  214. WORD command. 
  215.  
  216. ARG accepts a word or a list of words typed after the
  217. program name when it is started at the Shell. WORDS returns
  218. the number of separate words in a string of text, and WORD
  219. returns a particular word. See how simple ARexx programming
  220. is compared to the likes of C or BASIC?
  221.  
  222. Once we have a script that will create an icon from a given
  223. list of files, all we need is a way to easily get the list.
  224. Here's the ARexx script:
  225.  
  226.  
  227. /* Copy program two */
  228.  
  229. address command
  230.  
  231. arg list_of_names
  232.  
  233. do i=1 to words(list_of_names)
  234.  
  235.     name = word(list_of_names,i) || ".info"
  236.  
  237.     com="copy dummy.info" name
  238.  
  239.     com
  240.  
  241. end
  242.  
  243.  
  244.  
  245. Now to get the names of all the sound samples into a text
  246. file, enter this at the Shell:
  247.  
  248.  
  249. list lformat %n > ram:filelist
  250.  
  251. This performs a normal directory listing, but only with the
  252. file names, and then redirects the list into a file in the
  253. ram disk.
  254.  
  255. Unfortunately we can't simply run the ARexx script using
  256. this list, like this:
  257.  
  258. rx copy.rexx <ram:filelist
  259.  
  260. as the indirection doesn't seem to work. Instead we have to
  261. adjust the script to open up the text of the filelist
  262. itself. This time the script also include the necessary list
  263. command built-in.
  264.  
  265.  
  266.  
  267.  
  268.  
  269. /* Copy program three */
  270.  
  271. address command
  272.  
  273. "list lformat %s >t:filelist"
  274.  
  275.  
  276. call open(filepointer,"t:filelist","r")
  277.  
  278. do while ~eof(filepointer)
  279.  
  280.     name=readln(filepointer)  || ".info"
  281.  
  282.     com="copy dummy.info" name
  283.  
  284.     com
  285.  
  286. end
  287.  
  288. call close (filepointer)
  289.  
  290.  
  291.  
  292. And that's it!  This script will create a temporary list of
  293. all the files in the T:  directory, and then go through them
  294. one by one, copying the dummy.info icon with a new name.
  295. This should give all the files in the directory an icon -
  296. problem solved.
  297.  
  298.  
  299.  
  300.  
  301.  
  302. Table 1 - Common ARexx Commands
  303.  
  304.  
  305.  
  306.  
  307. SAY             Display text or the contents of a variable on
  308.                 screen. To see what difference quotes make,
  309.                 try the following:
  310.  
  311.                     again=10
  312.                     say Hello World
  313.                     say "Hello, World"
  314.                     say Hello, World again
  315.                     say "Hello, World again"        
  316.  
  317.             
  318.  
  319.  
  320.  
  321. DO / END        Packet up several lines. You can also create
  322.                 loops, like this:
  323.  
  324.  
  325.                     do 10
  326.                         say "Hello"
  327.                     end
  328.  
  329.                     
  330.                     do i=1 to 10 by 2
  331.                         say i
  332.                     end
  333.  
  334.  
  335.  
  336.  
  337. IF THEN ELSE
  338.  
  339.     
  340.                 Conditional testing. 
  341.  
  342.  
  343.                     if 1=1 then say "Phew, that was close"
  344.  
  345.  
  346.                     if x=19 then do
  347.                                     say "Well I never"
  348.                                     say "Good old X, eh?"
  349.                                  end
  350.                     else
  351.                           say "too bad"           
  352.  
  353.  
  354. ||              Concatanate two strings, without spaces.
  355.                 For example,
  356.  
  357.                 string1 = Hello World
  358.                 string2 = Hello || World
  359.                 say string1
  360.                 say string2
  361.  
  362.  
  363.  
  364. -----
  365.  
  366. pictures: novmast1.iff
  367. caption: A text editor such as Cygnus Ed makes editing ARexx
  368. scripts so much easier than Commodore's ED.